﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace QD.Snapshot.Core.DAL
{
    public class Product : DAL.DAO
    {
        public static int UpdateTime(string copyform, string ids, string article_ids = "")
        {
            int count = 0;
            if (!string.IsNullOrEmpty(ids))
            {
                string sql = string.Format("update sql_s003.dbo.vifo_snap_product_m set snapshotTime{0}=getdate() where productid>0 and productid in ({1})", copyform, ids);
                count += SqlHelper2013.ExecuteNonQuery(sql);
            }
            if (!string.IsNullOrEmpty(article_ids))
            {
                string sql = string.Format("update sql_s003.dbo.vifo_snap_product_m set snapshotTime{0}=getdate() where articleid>0 and articleid in ({1})", copyform, article_ids);
                count += SqlHelper2013.ExecuteNonQuery(sql);
            }
            return count;
        }

        public static List<Model.Product> GetList(int columnid, string copyform, int pageSize = 10, int userid = 0, int hours = 24)
        {
            StringBuilder sb = new StringBuilder();
            List<Model.Product> list = null;
            if (columnid == 1)
            {
                //微推广截图
                sb.AppendFormat("select top {0} productid,articleid,userid,keywords,SiteName,SubHeading from sql_s003.dbo.vifo_snap_product_m where columnid=1 and Deleted=0 and (WebSite_SiteCreateTime>GETDATE() or WebSite_WjbExpiredTime>GETDATE())", pageSize);
                if (userid > 0)
                    sb.AppendFormat(" and userid={0}", userid);

                sb.AppendFormat(" and DATEDIFF(hh,isnull(SnapshotTime{0},'1900-01-01'),getdate())>{1}", copyform, hours);
                sb.AppendFormat(" order by snapshotTime{0}", copyform);
            }
            else
            {
                //商汇通截图              
                sb.AppendFormat("select top {0} productid,articleid,userid,keywords,SiteName,SubHeading from sql_s003.dbo.vifo_snap_product_m where columnid!=1 and Deleted=0 and (WebSite_ExpiredTime>GETDATE() or WebSite_WjbExpiredTime>GETDATE())", pageSize);
                if (userid > 0)
                    sb.AppendFormat("and userid={0}", userid);

                if (!copyform.Equals("0"))  //360、搜狗、Bing、中国搜索,间隔30个小时截图，防止百度被顶下去
                    sb.AppendFormat(" and DATEDIFF(hh,isnull(SnapshotTime{0},'1900-01-01'),getdate())>{1}", copyform, hours);

                sb.AppendFormat(" order by snapshotTime{0}", copyform);
            }

            try
            {
                using (SqlDataReader dr = SqlHelper2013.ExecuteReader(CommandType.Text, sb.ToString(), null))
                {
                    if (dr != null)
                    {
                        if (dr.HasRows)
                            list = new List<Model.Product>();
                        while (dr.Read())
                        {
                            Model.Product model = new Model.Product();
                            model.productid = Convert.ToInt32(dr["productid"]);
                            model.articleid = Convert.ToInt32(dr["articleid"]);
                            model.userid = Convert.ToInt32(dr["userid"]);
                            model.sitename = Convert.ToString(dr["SiteName"]);
                            model.keywords = GetArrayList(dr["keywords"]);
                            model.subheading = GetArrayList(dr["subheading"]);

                            model.itemid = model.productid > 0 ? model.productid : model.articleid;
                            list.Add(model);
                        }
                        dr.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Pigeon.Utility.Log.Write(string.Format("error_{0}.log", DateTime.Now.ToString("yyyyMMdd")), string.Format("获取产品错误: {0} 方法：DAL.Product.GetList()", ex.Message));
            }
            return list;
        }

        static List<string> GetArrayList(object keyList)
        {
            if (keyList == null || keyList == DBNull.Value)
                return null;

            string[] arr = keyList.ToString().Split(',');

            List<string> list = new List<string>();
            foreach (string keyword in arr)
            {
                if (!list.Contains(keyword))
                    list.Add(keyword);
            }
            return list;
        }
    }
}